﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging;
using System.Windows;

namespace _05_微软Mvvm框架.ViewModels;

public class MainViewModel : ObservableObject
{
    public RelayCommand<string?> ShowCommand { get; set; }

    private string? _message;
    public string? Message
    {
        get { return _message; }
        set
        {
            _message = value;
            OnPropertyChanged();
        }
    }

    public MainViewModel()
    {
        // 注册一个消息
        WeakReferenceMessenger.Default.Register<string, string>(this,
            "Token1", (s, e) =>
        {
            MessageBox.Show(e);
        });
        ShowCommand = new RelayCommand<string?>(Show);
    }

    public void Show(string? content)
    {
        Message = "接到通知";
        // 给Token1发消息
        WeakReferenceMessenger.Default.Send(content ?? "", "Token1");
    }
}
